package com.adidas.util;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.List;

import com.adidas.bean.AdidasHttpBean;

public class AdidasHttpUtil {
	public static String acceptHtml = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3";
	public static String userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.XXXX.97 Safari/537.36";
	public static String contentTypeForm = "application/x-www-form-urlencoded; charset=UTF-8";
	
	public static AdidasHttpBean nikeHttpGet(String url,AdidasHttpBean adidasHttpBean) throws Exception{
		HttpClient httpClient = adidasHttpBean.getHttpClient();
		URI uri = new URI(url);
		HttpRequest httpGetRequest = HttpRequest.newBuilder(uri)
				.header("User-Agent",adidasHttpBean.getUserAgent())
    			.header("Accept",acceptHtml)
    			.header("Accept-Encoding", "")
    			.GET()
                .build();
		HttpResponse<String> resp =
				httpClient.send(httpGetRequest, HttpResponse.BodyHandlers.ofString());
		System.out.println(resp.body());
		adidasHttpBean.setReturnContent(resp.body());
		List<String> list = resp.headers().allValues("Set-Cookie");
        for(String temp : list) {
        	System.out.println(temp);
        	if(temp.startsWith("SESSIONID")) {
        		adidasHttpBean.setSessionId(temp.split(";")[0]);
        	}else if(temp.startsWith("recommend_tool_cookie_id")) {
        		adidasHttpBean.setRecommend_tool_cookie_id_v1(temp.split(";")[0]);
        	}else if(temp.startsWith("recommend_tool_member_id")) {
        		adidasHttpBean.setRecommend_tool_member_id_v1(temp.split(";")[0]);
        	}else if(temp.startsWith("l_b_s")) {
        		adidasHttpBean.setL_b_s(temp.split(";")[0]);
        	}else if(temp.startsWith("XSRF-TOKEN")) {
        		adidasHttpBean.setXsrf_token(temp.split(";")[0]);
        	}
        }
		return adidasHttpBean;
	}
	
	public static AdidasHttpBean nikeHttpPost(String url,String request_Body,AdidasHttpBean adidasHttpBean) throws Exception{
		URI uri = new URI(url);
		HttpClient httpClient = adidasHttpBean.getHttpClient();
		HttpRequest.Builder httpBuilder = HttpRequest.newBuilder(uri);
		if(adidasHttpBean.getXsrf_token()!=null && !"".equals(adidasHttpBean.getXsrf_token())) {
			String x_csrf_token = adidasHttpBean.getXsrf_token().split("=")[1];
			httpBuilder.header("X-CSRF-TOKEN",x_csrf_token);
			httpBuilder.header("Cookie",x_csrf_token);
		}
		if(adidasHttpBean.getContent_type()!=null && !"".equals(adidasHttpBean.getContent_type())) {
			httpBuilder.header("Content-Type",adidasHttpBean.getContent_type());
		}
		HttpRequest httpPostRequest = httpBuilder
				.header("User-Agent",adidasHttpBean.getUserAgent())
    			.header("Accept","*/*")
    			.header("Accept-Encoding", "")
    			.POST(HttpRequest.BodyPublishers.ofString(request_Body))
                .build();
		HttpResponse<String> resp =
				httpClient.send(httpPostRequest, HttpResponse.BodyHandlers.ofString());
		System.out.println(resp.body());
		adidasHttpBean.setReturnContent(resp.body());
		//cookie
		List<String> list = resp.headers().allValues("Set-Cookie");
        for(String temp : list) {
        	System.out.println(temp);
        	if(temp.startsWith("SESSIONID")) {
        		adidasHttpBean.setSessionId(temp.split(";")[0]);
        	}else if(temp.startsWith("recommend_tool_cookie_id")) {
        		adidasHttpBean.setRecommend_tool_cookie_id_v1(temp.split(";")[0]);
        	}else if(temp.startsWith("recommend_tool_member_id")) {
        		adidasHttpBean.setRecommend_tool_member_id_v1(temp.split(";")[0]);
        	}else if(temp.startsWith("l_b_s")) {
        		adidasHttpBean.setL_b_s(temp.split(";")[0]);
        	}else if(temp.startsWith("XSRF-TOKEN")) {
        		adidasHttpBean.setXsrf_token(temp.split(";")[0]);
        	}
        }
		return adidasHttpBean;
	}
}